import { BaseViewCmp } from "./BaseViewCmp";
import UI_cmp_obj_transform3d from "../../../ui/Main/UI_cmp_obj_transform3d";
import { Data_Transform3D } from "../../debug_inject/common/Data_Transform3D";
import { EObjCmpType } from "../../debug_inject/common/EObjCmpType";
import { BaseCmpData } from "../../debug_inject/common/BaseCmpData";
import UI_cmp_vec3 from "../../../ui/Main/UI_cmp_vec3";
import { Data_Vec3 } from "../../debug_inject/common/Data_Vec3";
import { DebuggerManager } from "../../debugger/DebuggerManager";
import { ViewDrag } from "../view/ViewDrag";
import { DataHelper } from "../utils/DataHelper";

export class CmpTransform3D extends BaseViewCmp {

    public ui: UI_cmp_obj_transform3d;

    private _cacheData: Data_Transform3D;
    private _dragList: ViewDrag[];

    constructor(objId: number) {
        super(EObjCmpType.Transform3D, objId);
        this.ui = UI_cmp_obj_transform3d.createInstance();

        this._dragList = [];
        {
            let viewDrag = new ViewDrag(this.ui.m_position.m_x.m_img_bg);
            viewDrag.onDrag = Laya.Handler.create(this, (x: number, y: number) => {
                this._cacheData.pos.x += x * 0.1 + y * 0.1;
                this._SetNum(this.ui.m_position.m_x.m_text_input, this._cacheData.pos.x);
                let newPos = this._PaseVec3(this.ui.m_position);
                DebuggerManager.instance.SetLocalPos(this.objId, newPos);
            }, null, false);
            this._dragList.push(viewDrag);
        }
        {
            let viewDrag = new ViewDrag(this.ui.m_position.m_y.m_img_bg);
            viewDrag.onDrag = Laya.Handler.create(this, (x: number, y: number) => {
                this._cacheData.pos.y += x * 0.1 + y * 0.1;
                this._SetNum(this.ui.m_position.m_y.m_text_input, this._cacheData.pos.y);
                let newPos = this._PaseVec3(this.ui.m_position);
                DebuggerManager.instance.SetLocalPos(this.objId, newPos);
            }, null, false);
            this._dragList.push(viewDrag);
        }
        {
            let viewDrag = new ViewDrag(this.ui.m_position.m_z.m_img_bg);
            viewDrag.onDrag = Laya.Handler.create(this, (x: number, y: number) => {
                this._cacheData.pos.z += x * 0.1 + y * 0.1;
                this._SetNum(this.ui.m_position.m_z.m_text_input, this._cacheData.pos.z);
                let newPos = this._PaseVec3(this.ui.m_position);
                DebuggerManager.instance.SetLocalPos(this.objId, newPos);
            }, null, false);
            this._dragList.push(viewDrag);
        }

        {
            let viewDrag = new ViewDrag(this.ui.m_rotation.m_x.m_img_bg);
            viewDrag.onDrag = Laya.Handler.create(this, (x: number, y: number) => {
                this._cacheData.rot.x += x * 0.1 + y * 0.1;
                this._SetNum(this.ui.m_rotation.m_x.m_text_input, this._cacheData.rot.x);
                let newRot = this._PaseVec3(this.ui.m_rotation);
                DebuggerManager.instance.SetLocalRot(this.objId, newRot);
            }, null, false);
            this._dragList.push(viewDrag);
        }
        {
            let viewDrag = new ViewDrag(this.ui.m_rotation.m_y.m_img_bg);
            viewDrag.onDrag = Laya.Handler.create(this, (x: number, y: number) => {
                this._cacheData.rot.y += x * 0.1 + y * 0.1;
                this._SetNum(this.ui.m_rotation.m_y.m_text_input, this._cacheData.rot.y);
                let newRot = this._PaseVec3(this.ui.m_rotation);
                DebuggerManager.instance.SetLocalRot(this.objId, newRot);
            }, null, false);
            this._dragList.push(viewDrag);
        }
        {
            let viewDrag = new ViewDrag(this.ui.m_rotation.m_z.m_img_bg);
            viewDrag.onDrag = Laya.Handler.create(this, (x: number, y: number) => {
                this._cacheData.rot.z += x * 0.1 + y * 0.1;
                this._SetNum(this.ui.m_rotation.m_z.m_text_input, this._cacheData.rot.z);
                let newRot = this._PaseVec3(this.ui.m_rotation);
                DebuggerManager.instance.SetLocalRot(this.objId, newRot);
            }, null, false);
            this._dragList.push(viewDrag);
        }

        {
            let viewDrag = new ViewDrag(this.ui.m_scale.m_x.m_img_bg);
            viewDrag.onDrag = Laya.Handler.create(this, (x: number, y: number) => {
                this._cacheData.scale.x += x * 0.1 + y * 0.1;
                this._SetNum(this.ui.m_scale.m_x.m_text_input, this._cacheData.scale.x);
                let newScale = this._PaseVec3(this.ui.m_scale);
                DebuggerManager.instance.SetLocalScale(this.objId, newScale);
            }, null, false);
            this._dragList.push(viewDrag);
        }
        {
            let viewDrag = new ViewDrag(this.ui.m_scale.m_y.m_img_bg);
            viewDrag.onDrag = Laya.Handler.create(this, (x: number, y: number) => {
                this._cacheData.scale.y += x * 0.1 + y * 0.1;
                this._SetNum(this.ui.m_scale.m_y.m_text_input, this._cacheData.scale.y);
                let newScale = this._PaseVec3(this.ui.m_scale);
                DebuggerManager.instance.SetLocalScale(this.objId, newScale);
            }, null, false);
            this._dragList.push(viewDrag);
        }
        {
            let viewDrag = new ViewDrag(this.ui.m_scale.m_z.m_img_bg);
            viewDrag.onDrag = Laya.Handler.create(this, (x: number, y: number) => {
                this._cacheData.scale.z += x * 0.1 + y * 0.1;
                this._SetNum(this.ui.m_scale.m_z.m_text_input, this._cacheData.scale.z);
                let newScale = this._PaseVec3(this.ui.m_scale);
                DebuggerManager.instance.SetLocalScale(this.objId, newScale);
            }, null, false);
            this._dragList.push(viewDrag);
        }

    }

    _OnDispose() {
        for (let drag of this._dragList) {
            drag.Dispose();
        }
        this._dragList = [];
    }

    private async _OnTextEnter() {
        let newPos = this._PaseVec3(this.ui.m_position);
        if (!this._CheckSame(newPos, this._cacheData.pos)) {
            DebuggerManager.instance.SetLocalPos(this.objId, newPos);
            this._cacheData.pos = newPos;
        }

        let newRot = this._PaseVec3(this.ui.m_rotation);
        if (!this._CheckSame(newRot, this._cacheData.rot)) {
            DebuggerManager.instance.SetLocalRot(this.objId, newRot);
            this._cacheData.rot = newRot;
        }

        let newScale = this._PaseVec3(this.ui.m_scale);
        if (!this._CheckSame(newScale, this._cacheData.scale)) {
            DebuggerManager.instance.SetLocalScale(this.objId, newScale);
            this._cacheData.scale = newScale;
        }
    }

    private _CheckSame(left: Data_Vec3, right: Data_Vec3) {
        return left.x == right.x && right.y == left.y && right.z == left.z;
    }

    private _PaseVec3(cmpUI: UI_cmp_vec3): Data_Vec3 {
        let data = new Data_Vec3();
        data.x = parseFloat(cmpUI.m_x.m_text_input.text);
        data.y = parseFloat(cmpUI.m_y.m_text_input.text);
        data.z = parseFloat(cmpUI.m_z.m_text_input.text);
        return data;
    }

    _OnRefresh(data: BaseCmpData) {
        if (this._cacheData != null) {
            let oldJson = JSON.stringify(this._cacheData);
            let newJson = JSON.stringify(data.data);
            if (oldJson == newJson) {
                return;
            }
        }

        this._cacheData = data.data;

        this._SetVec3(this.ui.m_position, this._cacheData.pos);
        this._SetVec3(this.ui.m_rotation, this._cacheData.rot);
        this._SetVec3(this.ui.m_scale, this._cacheData.scale);
    }

    private _SetVec3(cmpUI: UI_cmp_vec3, data: Data_Vec3) {
        this._SetNum(cmpUI.m_x.m_text_input, data.x);
        this._SetNum(cmpUI.m_y.m_text_input, data.y);
        this._SetNum(cmpUI.m_z.m_text_input, data.z);
    }

    private _SetNum(inputText: fgui.GTextInput, num: number) {
        inputText.text = DataHelper.NumToStr(num);
        inputText.on(Laya.Event.ENTER, this, this._OnTextEnter);
    }

}